//
//  JYJBigCollectionViewCell.m
//  HappyListen
//
//  Created by 冀永金 on 16/8/29.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJBigCollectionViewCell.h"
#import "JYJTAMacro.h"
#import "BaseTableView.h"
#import "BaseView.h"
#import "JYJTableHeadView.h"
#import "JYJRecommendTableViewCell.h"
#import "JYJSpecialFocusTableViewCell.h"
#import "JYJNetWorkParser.h"
#import "JYJEventListModel.h"
#import "JYJMusicListTableViewCell.h"
#import "JYJSpecialListModel.h"
#import "JYJSepcialListTableViewCell.h"

#import "JYJBestMuscicListModel.h"
#import "JYJMusicsModel.h"
#import "JYJMusicsContentModel.h"
#import "JYJChannelModel.h"
#import "JYJVCOfDiscover.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"
#import "JYJMusicPlayerViewController.h"
#import "JYJDataBaseTool.h"
#import "JYJDataTools.h"
#import "MJRefresh.h"
@interface JYJBigCollectionViewCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray <JYJEventListModel *>*enentMarry;
@property (nonatomic, strong) JYJBestMuscicListModel *model;
@end
@implementation JYJBigCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00]; 
        [self createTableView];
        [self createHeadView];
        [self handleData];
    }
    return self;
}
- (void)handleData {
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/discovery.jhtml" andBody:@"json=%7B%7D" andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
        self.model = [JYJBestMuscicListModel modelWithDic:result[@"data"]];
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];

}
- (void)createTableView {
    self.tableView = [[BaseTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.contentView addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    [self.tableView setSeparatorColor:[UIColor clearColor]];
    [self.tableView registerClass:[JYJRecommendTableViewCell class] forCellReuseIdentifier:@"Recommend"];
    [self.tableView registerClass:[JYJSepcialListTableViewCell class] forCellReuseIdentifier:@"SpecialList"];
    [self.tableView registerClass:[JYJMusicListTableViewCell class] forCellReuseIdentifier:@"MusicList"];
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 下拉刷新动画
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        NSString *name = [NSString stringWithFormat:@"list_load_%d",i];
        UIImage *image = [UIImage imageNamed:name];
        [marr addObject:image];
    }
//    /** 普通闲置状态 */
//    MJRefreshStateIdle = 1,
//    /** 松开就可以进行刷新的状态 */
//    MJRefreshStatePulling,
//    /** 正在刷新中的状态 */
//    MJRefreshStateRefreshing,

    [header setImages:marr forState:MJRefreshStateIdle];
    [header setImages:marr forState:MJRefreshStatePulling];
    [header setImages:marr forState: MJRefreshStateRefreshing];
    self.tableView.mj_header = header;
   header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
}
- (void)loadNewData {
    [self handleData];
    [self.tableView.mj_header endRefreshing];
}
- (void)createHeadView {
    JYJTableHeadView *head = [[JYJTableHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    self.tableView.tableHeaderView = head;
    
    
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.tableView.frame = self.contentView.bounds;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2 + self.model.best_musiclist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        JYJRecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Recommend" forIndexPath:indexPath];
        return cell;
    }else if (indexPath.row == 1) {
        JYJSepcialListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SpecialList" forIndexPath:indexPath];
        cell.block= self.handleAction;
        return cell;
    }
    else {
        JYJMusicListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicList" forIndexPath:indexPath];
        cell.handleAction = self.handleBlock;
        cell.handleVC = self.handleVC;
        JYJChannelModel *model = self.model.best_musiclist[indexPath.row - 2].model;
        [cell setChanneModel:model];
        JYJMusicsModel *musicModel = self.model.best_musiclist[indexPath.row - 2];
        [cell setMusicModel:musicModel];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 580;
    }else {
        return 220;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        JYJVCOfDiscover *discover = (JYJVCOfDiscover *) self.nextResponder.nextResponder.nextResponder;
        JYJVCOfFeaturedTopThreeDetails *details = [[JYJVCOfFeaturedTopThreeDetails alloc]init];
        discover.tabBarController.tabBar.hidden = YES;
        discover.hidesBottomBarWhenPushed = YES;
        [discover.navigationController pushViewController:details animated:YES];
        discover.hidesBottomBarWhenPushed = NO;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
